package com.qf.dao.Impl;

import com.qf.dao.StuInfoDao;
import com.qf.entity.StuInfo;
import com.qf.util.DatasoureUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author a1275872671
 * @version v1.0
 * @project guanqipeng
 * @package com.qf.dao.Impl
 * @company
 * @date 2024/4/30 10:22
 */
public class StuInfoDaoImpl implements StuInfoDao {
    private QueryRunner qr=new QueryRunner(DatasoureUtil.getDataSource());
    @Override
    public List<StuInfo> selectAll() {

        String sql="select * from stulnfo";
        try {
            return qr.query(sql,new BeanListHandler<>(StuInfo.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public void insert(StuInfo stuInfo) {
        String sql="insert into stulnfo (stuname,shuno,pwd,idcard,startdate,status) values(?,?,?,?,?,?)";
        Object[] params={stuInfo.getStuName(),stuInfo.getShuno(),stuInfo.getPwd(),stuInfo.getIdcard(),stuInfo.getStartDate(),stuInfo.getStatus()};
        try {
            qr.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long selectCount() {
        try {
            return qr.query( "select count(*) from stulnfo",new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<StuInfo> selectByPage(int uid, int ps) {
        String sql="select id,stuName,shuno,pwd,idcard,startDate,status from stulnfo limit ?,?";
        try {
            return qr.query(sql,new BeanListHandler<>(StuInfo.class),uid,ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id)  {
        try {
            qr.update("delete  from stulnfo where id=?",id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public StuInfo selectById(int id) {
        String sql="select * from stulnfo where id=?";
        try {
            return qr.query(sql,new BeanHandler<>(StuInfo.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateStu(StuInfo stuInfo) {
        String sql = "update stulnfo set stuName = ?,shuno = ?,pwd = ?,idcard = ?,startDate = ?,status = ? where id = ?";
        Object[] params = {stuInfo.getStuName(),stuInfo.getShuno(),stuInfo.getPwd(),stuInfo.getIdcard(),stuInfo.getStartDate(),stuInfo.getStatus(),stuInfo.getId()};
        try {
            qr.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
